import time, struct
import pc_sock as psk
import image_to_txt as itt


Path = 'E:/Learn/2.project_code/tftlcd/image/2.jpg'

def data_bin(data, len):
    dat = struct.pack('>H', data[0])

    for i in range(1, len):
       dat = dat + struct.pack('>H', data[i])

    # 判断发送数据类型,如果是字节(bytes)直接发送
    # 其他新式如list、int都需要转化成str进行发送
    # 不建议第二种发送方法
    print(type(dat), '\n', dat)
    if type(dat) is bytes:
        psk.client(mode="UDP", addr=('192.168.228.42', 7788), data=dat, defa=True)
    else:
        psk.client(mode="UDP", addr=('192.168.228.42', 7788), data=str(dat), defa=True)


# 将数列分割,image_width*45 = 0x2A30
def seque_divise(list_data, iamge_width):
    # 将数据转换成0x12格式传输,divise_len需要*2
    divise_len = iamge_width*3
    length = len(list_data)
    lengt = length // divise_len

    for i in range(0, lengt + 1):
        if i == lengt:
            dat = list_data[(i * divise_len):(length - 1)]
        else:
            dat = list_data[(i * divise_len):(i + 1) * divise_len]

        # 1.将数据转换成b'/12'格式传输
        if len(dat) != 0:
            data_bin(dat, len(dat))
        # # 2.将数据转换成0x12格式传输
        # if len(dat) != 0:
        #     psk.client(mode="UDP", addr=('192.168.173.42', 7788), data=str(dat), defa=True)


if __name__ == '__main__':
    # 1.将数据转换成b'/12'格式传输，数据显示错误
    # 传输函数地址
    image, image_width, image_height = itt.direct_read_image(Path)
    # 将rgb三通道转换成ili9431的一通道

    image_conv = itt.color_rgb565(image)
    print(len(image_conv), image_width, image_height)
    # 将一通道数据转换成0x个数字符
    seque_divise(image_conv, image_width)

    # 2.将数据转换成0x12格式传输
    # image, image_width, image_height = itt.direct_read_image(Path)
    # image_conv = itt.color_rgb565(image)
    # image_data = itt.seque_conv(image_conv)
    # seque_divise(image_data, image_width)


